/*
 * home.c
 *
 *  Created on: 2021年10月28日
 *      Author: 王慧云
 */

#include "home.h"
#include <math.h>
#include <stdlib.h>

/*速度标签句柄*/
lv_obj_t* label_speed_val;
/*速度显示arc*/
lv_obj_t* arc_speed;
/*速度标签*/
lv_obj_t* label_SetValue;
/*显示pid标签句柄*/
lv_obj_t* label_PID;
/*占空比bar句柄*/
lv_obj_t* bar_SetValue;
/*电机运行状态指示灯*/
lv_obj_t* LED ;
/*创建一个定时器*/
lv_timer_t* refresh_timer;
/*创建一个键盘事件组*/
lv_group_t* home_keypadGroup;

/**
 * 定时器回调
 */
void my_timer(lv_timer_t * timer)
{
	int adcVal = 0;//读一下ADC值	
	adcVal = get_adc_average(hadc1, ADC_CHANNEL_12,10);
	if(adcVal < 5)
		adcVal = 5;
	if(adcVal>4005)
		adcVal = 4005;
	if(motor_IsPIDenable()) //是否是pid控制 如果是pid控制则使用转速调节/显示否则就是占空比
	{
		if(motor_get_CtrlMode() == KNOB) //是否位旋钮调节，旋钮调节可以直接设置值其他调节不可以
		{
			adcVal = (int16_t)((motor_get_maxSpeed()/4000.0)*adcVal -(5*motor_get_maxSpeed()/4000.0));
			if(motor_get_runDir()) //如果是反向，则设置
				adcVal = -adcVal;
			motor_set_pid_target(adcVal);
			lv_label_set_text_fmt(bar_SetValue,"%d",adcVal);
		}
		else
		{
			lv_label_set_text_fmt(bar_SetValue,"%d",(int16_t)motor_get_pid_target());
		}
		lv_label_set_text(label_SetValue, "给定(RPM)");
	}
	else
	{
		if(motor_get_CtrlMode() == KNOB)
		{
			adcVal = (uint16_t)(adcVal*0.25-1.25);
			lv_label_set_text_fmt(bar_SetValue,"%d.%d%%",adcVal/10,adcVal%10);
			motor_set_dutyCycle(adcVal,motor_get_runDir());			
		}
		else
		{
			lv_label_set_text_fmt(bar_SetValue,"%d.%d%%",motor_get_dutyCycle()/10,motor_get_dutyCycle()%10);
			
		}
		lv_label_set_text(label_SetValue, "给定(占空比)");
	}

	/*判断电机运行状态*/
	switch(motor_get_runStatus())
	{
		case 0:
			lv_led_off(LED);
			break;
		case 1:
			lv_led_set_color(LED, lv_palette_main(LV_PALETTE_GREEN));
			lv_led_on(LED);
			break;
		case 2:
			lv_led_set_color(LED, lv_palette_main(LV_PALETTE_RED));
			lv_led_toggle(LED);
			break;
	}

	/*判断电机运行方向*/
	/*用label显示速度大小*/
	lv_label_set_text_fmt(label_speed_val,"%d",(int)motor_get_speed());
	/*用圆弧arc的方式表示速度大小*/
	uint16_t arc_val = 100.0/motor_get_maxSpeed() * abs((int)motor_get_speed());
	lv_arc_set_value(arc_speed, arc_val);
	switch(motor_get_CtrlMode())
	{
		case KNOB:
			lv_label_set_text(label_PID, "KNOB");
			break;
		case IO:
			lv_label_set_text(label_PID, "IO");
			break;
		case RS:
			lv_label_set_text(label_PID, "UART");
			break;
		default:
			break;
	}

}

/*主页退出程序回调*/
static void home_exit_cb(void * scr)
{
	lv_timer_del(refresh_timer);
	lv_group_del(home_keypadGroup);
	lv_obj_del(scr);
	menu(0);
}

/*处理主页按键事件**/
static void home_cb(lv_event_t * event)
{
	switch(lv_indev_get_key(lv_indev_get_act()))
	{
		case LV_KEY_ENTER:
			//printf("KEY ENTER \r\n");
			lv_async_call(home_exit_cb, lv_scr_act());
			break;
		case LV_KEY_LEFT:
			if(motor_get_CtrlMode() != RS)
				motor_set_RunDir(motor_get_runDir()>0?0:1);					
			break;
		case LV_KEY_RIGHT:
			if(motor_get_CtrlMode() != RS)
				motor_set_RunStatus(motor_get_runStatus() > 0 ? 0 : 1);
			break;
		//case LV_KEY_NEXT:
			//break;
		default:
			break;
	}
}



/**
 * @brief 主界面
 * @param
*/
void MotorCtrol_Home(void)
{
	/*创建一个首页屏幕*/
	lv_obj_t* home_scr = lv_obj_create(NULL);
	lv_scr_load(home_scr);
	lv_obj_add_event_cb(home_scr, home_cb, LV_EVENT_KEY , NULL);
	//lv_obj_add_flag(home_scr, LV_OBJ_FLAG_CLICKABLE);
	/*将屏幕和输入设备添加到组中*/
	/*创建一个组*/
	home_keypadGroup = lv_group_create();
	lv_group_add_obj(home_keypadGroup, home_scr);
	lv_indev_set_group(indev_keypad, home_keypadGroup);

	/*Create an Arc*/
    arc_speed = lv_arc_create(lv_scr_act());
    lv_obj_set_size(arc_speed, 67, 67);
    lv_arc_set_rotation(arc_speed, 135);
    lv_arc_set_bg_angles(arc_speed, 0, 270);
    lv_obj_remove_style(arc_speed, NULL, LV_PART_KNOB);
    lv_obj_clear_flag(arc_speed, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_align(arc_speed, LV_ALIGN_CENTER, -40, -3);
    lv_arc_set_value(arc_speed, 50);


    /*创建一个显示速度的标签*/
    label_speed_val = lv_label_create(lv_scr_act());
    lv_obj_align(label_speed_val, LV_ALIGN_CENTER, -40, -4);
    lv_label_set_text(label_speed_val,"000");
    /*创建一个标签显示"RPM"*/
    lv_obj_t* label_rpm = lv_label_create(lv_scr_act());
    lv_obj_align(label_rpm, LV_ALIGN_CENTER, -40, 6);
    lv_label_set_text(label_rpm, "rpm");
    /*创建一个标签显示"Speed"*/
    lv_obj_t* label_speed = lv_label_create(lv_scr_act());
    lv_obj_align(label_speed, LV_ALIGN_CENTER, -40, 30);
    lv_label_set_text(label_speed,"速度");
    lv_obj_set_style_text_font(label_speed,&simyou_14,0);
    /*创建一个标签显示"给定(占空比)"*/
    label_SetValue = lv_label_create(lv_scr_act());
    lv_obj_align(label_SetValue, LV_ALIGN_CENTER, 35, -27);
	if(motor_IsPIDenable() == 1)
		lv_label_set_text(label_SetValue, "给定(RPM)");
	else 
    	lv_label_set_text(label_SetValue, "给定(占空比)");
    lv_obj_set_style_text_font(label_SetValue, &simyou_14, 0);
    /*创建一个进度条显示给定值*/
    bar_SetValue = lv_label_create(lv_scr_act());
    lv_obj_set_size(bar_SetValue, 76,15);
    lv_obj_align(bar_SetValue, LV_ALIGN_CENTER, 40, -10);
	lv_obj_add_style(bar_SetValue, &style_label_2, LV_PART_MAIN);
	lv_obj_set_style_bg_color(bar_SetValue,lv_color_hex(0x87CEEB),LV_PART_MAIN);
    /*创建一个显示是否为PID调节的标签*/
    label_PID = lv_label_create(lv_scr_act());
    lv_obj_add_style(label_PID,&style_label_1, LV_PART_MAIN);
    lv_obj_align(label_PID, LV_ALIGN_CENTER, 40, 10);
    lv_obj_set_width(label_PID, 76);
    lv_label_set_text(label_PID, "PID调节");
//    lv_obj_set_style_text_font(label_PID, &simyou_14, 0);
    /*创建一个标签显示"运行状态"*/
    lv_obj_t* label_Run = lv_label_create(lv_scr_act());
    lv_obj_add_style(label_Run, &style_label_2, LV_PART_MAIN);
    lv_obj_align(label_Run, LV_ALIGN_CENTER, 25, 30);
    lv_label_set_text(label_Run," 运行状态 ");
    lv_obj_set_style_text_font(label_Run, &simyou_14, 0);

    /*创建一个led灯*/
    LED = lv_led_create(lv_scr_act());
    lv_obj_align(LED, LV_ALIGN_CENTER,70,30);
    lv_obj_add_style(LED,&lv_style_led,LV_PART_MAIN);
    lv_led_off(LED);
    /*创建一个定时器用来刷新*/
    refresh_timer = lv_timer_create(my_timer, 20, NULL);
}



